import apiRequest from '@/utils/apiRequest';

/**
 * 获取EBOM列表
 * @param params - 查询参数
 * @returns - 返回EBOM分页列表
 */
export const getEBOMs = async (params) => {
  try {
    const response = await apiRequest.get('/EBOMs/search', { params });
    return response.data;
  } catch (error) {
    console.error('Failed to get EBOMs:', error);
    throw error;
  }
};

/**
 * 根据ID获取EBOM详情
 * @param id - EBOM ID
 * @returns - 返回EBOM详情
 */
export const getEBOMById = async (id) => {
  try {
    const response = await apiRequest.get(`/EBOMs/${id}`);
    return response.data;
  } catch (error) {
    console.error('Failed to get EBOM by ID:', error);
    throw error;
  }
};

/**
 * 创建EBOM
 * @param data - EBOM数据
 * @returns - 返回创建结果
 */
export const createEBOM = async (data) => {
  try {
    const response = await apiRequest.post('/EBOMs', data);
    return response.data;
  } catch (error) {
    console.error('Failed to create EBOM:', error);
    throw error;
  }
};

/**
 * 更新EBOM
 * @param id - EBOM ID
 * @param data - 更新数据
 * @returns - 返回更新结果
 */
export const updateEBOM = async (id, data) => {
  try {
    const response = await apiRequest.put(`/EBOMs/${id}`, data);
    return response.data;
  } catch (error) {
    console.error('Failed to update EBOM:', error);
    throw error;
  }
};

/**
 * 审核EBOM
 * @param id - EBOM ID
 * @param approvedBy - 审核人ID
 * @returns - 返回审核结果
 */
export const approveEBOM = async (id, approvedBy) => {
  try {
    const response = await apiRequest.patch(`/EBOMs/${id}/approve`, null, {
      params: { approvedBy }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to approve EBOM:', error);
    throw error;
  }
};

/**
 * 启用EBOM
 * @param id - EBOM ID
 * @param enabledBy - 启用操作人ID
 * @returns - 返回启用结果
 */
export const enableEBOM = async (id, enabledBy) => {
  try {
    const response = await apiRequest.patch(`/EBOMs/${id}/enable`, null, {
      params: { enabledBy }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to enable EBOM:', error);
    throw error;
  }
};

/**
 * 禁用EBOM
 * @param id - EBOM ID
 * @param disabledBy - 禁用操作人ID
 * @returns - 返回禁用结果
 */
export const disableEBOM = async (id, disabledBy) => {
  try {
    const response = await apiRequest.patch(`/EBOMs/${id}/disable`, null, {
      params: { disabledBy }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to disable EBOM:', error);
    throw error;
  }
};

/**
 * 创建EBOM新版本
 * @param id - EBOM ID
 * @param newVersion - 新版本号
 * @param createdBy - 创建人ID
 * @param changeReason - 变更原因
 * @returns - 返回创建结果
 */
export const createNewVersion = async (id, newVersion, createdBy, changeReason) => {
  try {
    const response = await apiRequest.post(`/EBOMs/${id}/new-version`, null, {
      params: {
        newVersion,
        createdBy,
        changeReason
      }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to create new EBOM version:', error);
    throw error;
  }
};

/**
 * 删除EBOM
 * @param id - EBOM ID
 * @returns - 返回删除结果
 */
export const deleteEBOM = async (id) => {
  try {
    const response = await apiRequest.delete(`/EBOMs/${id}`);
    return response.data;
  } catch (error) {
    console.error('Failed to delete EBOM:', error);
    throw error;
  }
};